package cn.jbolt.ec.retailer;

import cn.jbolt.base.JBoltBaseService;
import cn.jbolt.common.config.Msg;
import cn.jbolt.common.model.BecRetailer;
import cn.jbolt.common.model.BecRetailerOrder;
import cn.jbolt.ec.cloud.QnService;
import cn.jbolt.ec.order.OrderService;
import cn.jbolt.ec.util.MyUtil;
import com.alibaba.fastjson.JSON;
import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.util.Date;
import java.util.List;
/**
 * @author xieguo
 */
public class RetailerService extends JBoltBaseService<BecRetailer> {
    private BecRetailer dao = new BecRetailer().dao();
    @Override
    protected BecRetailer dao() {
        return dao;
    }

    @Inject
    OrderService orderService;


    /**
     * 分页获取商家/按商家名搜索商家
     * @param pageNumber
     * @param pageSize
     * @param pattern
     * @return
     */
    public Page<Record> retailer_info(Integer pageNumber, Integer pageSize,Integer type_id,String pattern){

        Kv param=Kv.by("pageNumber",pageNumber).set("pageSize",pageSize).set("pattern",pattern).set("type_id",type_id);

        return dbTemplate("ec.mall.shop.retailer.retailer_page",param).paginate(pageNumber,pageSize);
    }



    /**
     * 根据商家id获取信息
     * @param id
     * @return
     */
    public List<BecRetailer> retailer_info_by_id(Integer id){
        Kv param=Kv.by("id",id);
        return getCommonList("name,address,mobile,license_image,open_time,close_time",param);
    }

    /**
     * 获取商家订单信息
     * @param retailer_id
     * @param status
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public Page<BecRetailerOrder> retailer_order_info(Integer retailer_id, Integer status, Integer pageNumber, Integer pageSize){
        Kv param=Kv.by("retailer_id",retailer_id).set("status",status);
        return orderService.paginate("amount_total,amount_paid, id,order_id,consignee,order_time,address,mobile",param,pageNumber,pageSize,false);
    }


    // 商家确认结单（已送货）
    public int confirm(Integer retailer_id, String orders){
        int[] ids = JSON.parseObject(orders,int[].class);


        Kv cond=Kv.by("retailer_id",retailer_id).set("ids",ids).set("status",18);
        return dbTemplate("ec.mall.shop.retailer.delivery_confirm", cond).update();
    }


    /**
     * 获取商家名字
     * @param retailer_id
     * @return
     */
    public String get_retailer_name(Integer retailer_id){
        Kv param =Kv.by("id",retailer_id);
        return getCommonList("name",param).get(0).getName();
    }

    /**
     * 获取附近门店
     * @param pageNumber
     * @param pageSize
     * @param pattern
     * @return
     */
    public Page<Record> get_near(Integer pageNumber,Integer pageSize,String pattern){
        Kv param=Kv.by("pattern",pattern);
        return dbTemplate("ec.mall.shop.retailer.get_near",param).paginate(pageNumber,pageSize);
    }




    /*************************************************************
     * 后台管理方法
     * @author  heqingyu
     * ***********************************************************
     */

    /**
     * 获取后台主页数据
     * @param pageNum
     * @param pageSize
     * @param keywords
     * @return
     */
    public Page<BecRetailer> get_retailer_page(Integer pageNum, Integer pageSize, String keywords,boolean delete_status,Integer retailer_status){
        Kv conf = Kv.by("table",table());
        conf.setIfNotNull("keywords",keywords);
        conf.setIfNotNull("retailer_status",retailer_status);
        conf.setIfNotNull("delete_status",delete_status);
        if (delete_status)
            conf.set("retailer_status",null);
        return paginateBySqlTemplate("ec.mall.manage.retailer.get_retailer_page",conf,pageNum,pageSize);

    }

    /**
     * 获取 删除商家数量
     * @return
     */
    public Integer get_delete_count(){
        return getCount(Kv.by("status",1));
    }


    /**
     * 添加  商家 基本信息
     * @param becRetailer
     * @return
     */
    public Ret save_basic(BecRetailer becRetailer, String open_time, String close_time){

        if (becRetailer == null||open_time==null || close_time==null)
            return fail(Msg.PARAM_ERROR);
        String name=becRetailer.getName().trim();
        if (existsName(name)) {
            return fail(Msg.DATA_SAME_NAME_EXIST+"商品名:["+name+"]");
        }
        String mobile=becRetailer.getMobile().trim();
        if (existsName(mobile)) {
            return fail(Msg.DATA_SAME_NAME_EXIST+"手机号:["+name+"]");
        }

        long opentime= MyUtil.me.String_to_date(open_time).getTime();
        long closetime = MyUtil.me.String_to_date(close_time).getTime();
        if (opentime > closetime)
            return fail("开始时间必须小于关闭时间！");

        Long join_time = System.currentTimeMillis();

        becRetailer.setJoinTime(join_time);

        becRetailer.setOpenTime(opentime);
        becRetailer.setCloseTime(closetime);

        becRetailer.setStatus(2);


        boolean success=becRetailer.save();
        if(success){
            return SUCCESS;
        }
        return FAIL;



    }



    /**
     * 更新 商家基本信息
     * @param becRetailer
     * @return
     */
    public Ret update_basic(BecRetailer becRetailer, Integer type_id, String open_time, String close_time)  {


        if(becRetailer==null||notOk(becRetailer.getId())
                ||notOk(becRetailer.getName())||open_time==null || close_time==null){
            return fail(Msg.PARAM_ERROR);
        }
        if (type_id == null)
            return fail("未选商家类型");

        long opentime= MyUtil.me.String_to_date(open_time).getTime();
        long closetime = MyUtil.me.String_to_date(close_time).getTime();
        if (opentime > closetime)
            return fail("开始时间必须小于关闭时间！");

        becRetailer.setTypeId(type_id);
        becRetailer.setOpenTime(opentime);
        becRetailer.setCloseTime(closetime);
        return update(becRetailer);


    }


    /**
     *  更新商家 财务设置
     * @param becRetailer
     * @return
     */
    public Ret update_finance(BecRetailer becRetailer) {
        System.out.println(becRetailer);
        if(becRetailer==null||notOk(becRetailer.getId())){
            return fail(Msg.PARAM_ERROR);
        }

        return  update(becRetailer);
    }


    /**
     *  更新 商品图片
     */
    public Ret update_license_image(BecRetailer becRetailer, String imgUrl) {
        if (becRetailer == null || notOk(becRetailer.getId())
                || imgUrl == null) {
            return fail(Msg.PARAM_ERROR);
        }
        becRetailer.setLicenseImageOld(becRetailer.getLicenseImage());
        becRetailer.setLicenseImage(imgUrl);
        return update(becRetailer);
    }


    public Ret update_location(BecRetailer becRetailer){
        if (becRetailer == null || becRetailer.getId() == null)
            return fail("操作失败");
        return update(becRetailer);
    }


    /**
     *  设置 商家 状态
     *   0 审核通过
     *   1  删除
     *   2  待审核
     * @param retailer_id
     * @param status
     * @return
     */
    public Ret set_retailer_status(Integer retailer_id,Integer status){
        if (notOk(retailer_id))
            return fail("参数错误");
        BecRetailer becRetailer = findById(retailer_id);
        if (becRetailer == null)
            return fail("没有该商家，错误！");
        becRetailer.setStatus(status);
        return  update(becRetailer);
    }







    /**
     * 更新
     * @param becRetailer
     * @return
     */
    public Ret update(BecRetailer becRetailer){

        boolean success=becRetailer.update();
        if(success){
            //添加日志
            return SUCCESS;
        }
        return FAIL;
    }


    /**
     * 秒杀商品 添加时  一级选项数据源 选择商家
     * @return
     */
    public  List<BecRetailer> rush_commodity_select_retaile(){
        return getCommonList("id,name");
    }












}
